//
//  KFuViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/24.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "KFuViewController.h"

#import "KeFuView.h"
#import "HelpViewController.h"
@interface KFuViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *array;
    
}

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSDictionary *dic;

@end

@implementation KFuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self showNav:@"客服中心"];
    array = [NSMutableArray array];
    [self loadData];
    
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, 44*array.count-1+140) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = NO;
    [self.view addSubview:_tableView];
    
    KeFuView *imageView = [[[NSBundle mainBundle] loadNibNamed:@"KeFuView" owner:nil options:nil] lastObject];
    imageView.frame = CGRectMake(0, 0, ScreenWidth, 140);
    _tableView.tableHeaderView = imageView;
    
    imageView.labPhone.text = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"customerservice"] integerValue]];
    imageView.labDate.text = [_dic objectForKey:@"servicetime"];
    [imageView.btnTel addTarget:self action:@selector(clickTel) forControlEvents:UIControlEventTouchUpInside];
    
    
}
//联系客服
- (void)clickTel{
    NSString *phone = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"customerservice"] integerValue]];
    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
    UIWebView *web = [[UIWebView alloc] init];
    [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:web];
}

#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    NSDictionary *dic = array[indexPath.row];
    
    cell.textLabel.text = [dic objectForKey:@"question"];
    cell.textLabel.font = [UIFont systemFontOfSize:14.0];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.imageView.image = [UIImage imageNamed:@"问号"];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HelpViewController *help = [[HelpViewController alloc] init];
    NSDictionary *dic = array[indexPath.row];
    help.dic = dic;
    [self.navigationController pushViewController:help animated:YES];
}
//客服中心
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:token forKey:@"verifyToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    NSString *url = [NSString stringWithFormat:@"%@/App/Companyinfo/index",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"客服中心res:%@",responseObject);
        NSString *status = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"status"] integerValue]];
        if ([status integerValue] == 200) {
            array = [responseObject objectForKey:@"problem"];
            _dic = [responseObject objectForKey:@"info"];
        }
        
        [self loadUI];
        [_tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
